package com.sgb.trip.view.adapter;

import android.content.Context;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.bumptech.glide.Glide;
import com.sgb.trip.R;
import com.sgb.trip.databinding.TripItemFlightOrderBinding;
import com.sgb.trip.entity.FlightOrdersInfoEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.NumberUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Time:2021/6/18
 * Author:黄淋
 * Description:选择乘客时的adapter
 */
public class FlightOrdersAdapter extends BaseRecycleAdapter<FlightOrdersInfoEntity> {

    ArrayList<CountDownTimer> allTimer = new ArrayList<>();

    public FlightOrdersAdapter(Context context, List<FlightOrdersInfoEntity> datas) {
        super(context, R.layout.trip_item_flight_order, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, FlightOrdersInfoEntity entity) {
        TripItemFlightOrderBinding binding = DataBindingUtil.bind(holder.itemView);

        binding.viewLineFlight.setVisibility(View.GONE);
        binding.tvApplyPay.setVisibility(View.GONE);
        binding.tvOrderLeftTime.setText("");
        binding.tvOrderLeftTime.setVisibility(View.GONE);
        switch (entity.getOrderStatus()) {
            case 0://待支付
                binding.tvOrderLeftTime.setVisibility(View.VISIBLE);
                binding.tvOrderStatus.setText("待支付");
                binding.viewLineFlight.setVisibility(View.VISIBLE);
                binding.tvApplyPay.setVisibility(View.VISIBLE);
                binding.tvApplyPay.setOnClickListener(view -> {
                    if (onClickItem != null) onClickItem.onApplyPay(entity);
                });

                long leftTime = entity.getOrderTime() + 20 * 60 * 1000 - System.currentTimeMillis();//下单时间后20分钟为截止时间
                if (leftTime > 0) {
                    CountDownTimer timer = new CountDownTimer(leftTime, 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            String str = String.format("%s%s", "剩余", DateUtils.formatDate(millisUntilFinished, "mm分ss秒"));
                            binding.tvOrderLeftTime.setText(str);
                        }

                        @Override
                        public void onFinish() {
                            binding.tvOrderStatus.setText("已取消");
                            binding.tvOrderLeftTime.setText("因超时未支付订单已取消");
                            binding.viewLineFlight.setVisibility(View.GONE);
                            binding.tvApplyPay.setVisibility(View.GONE);
                        }
                    };
                    timer.start();
                    allTimer.add(timer);
                } else {
                    binding.tvOrderStatus.setText("已取消");
                    binding.tvOrderLeftTime.setText("因超时未支付订单已取消");
                    binding.viewLineFlight.setVisibility(View.GONE);
                    binding.tvApplyPay.setVisibility(View.GONE);
                }
                break;
            case 1://
                binding.tvOrderStatus.setText("待出票");
                break;
            case 2://
                binding.tvOrderStatus.setText("待出行");
                break;
            case 3://
                binding.tvOrderStatus.setText("已取消");
                binding.tvOrderLeftTime.setText("订单已取消");
                binding.tvOrderLeftTime.setVisibility(View.VISIBLE);
                break;
            case 4://
                binding.tvOrderStatus.setText("出票失败");
                break;
            case 5://
                binding.tvOrderStatus.setText("已完成");
                break;
            case 6://
                binding.tvOrderStatus.setText("已取消");
                binding.tvOrderLeftTime.setText("因超时未支付订单已取消");
                binding.tvOrderLeftTime.setVisibility(View.VISIBLE);
                break;
            case 7://
                binding.tvOrderStatus.setText("已关闭");
                break;
            case 8://
                binding.tvOrderStatus.setText("退款中");
                break;
            case 9://
                binding.tvOrderStatus.setText("已退款");
                break;
        }

        if (entity.getTicketsFlights() != null && entity.getTicketsFlights().size() == 1) {
            FlightOrdersInfoEntity.TicketsFlightsBean bean = entity.getTicketsFlights().get(0);
            binding.clFlightBack.setVisibility(View.GONE);
            binding.tvTo.setVisibility(View.GONE);
            binding.tvOrderPrice.setText("¥" + NumberUtil.trim0(bean.getTicketAmount()));
            binding.tvCityTo.setText(bean.getFromCity() + " → " + bean.getToCity());

            String startDate = DateUtils.formatDate(bean.getFromDateTime(), "MM月dd日");
            String arriveDate = DateUtils.formatDate(bean.getToDateTime(), "MM月dd日");
            if (startDate.equals(arriveDate)) {
                binding.tvTimeTo.setText(DateUtils.formatDate(bean.getFromDateTime(), "MM月dd日 HH:mm") + " 至 " + DateUtils.formatDate(bean.getToDateTime(), "HH:mm"));
            } else {
                binding.tvTimeTo.setText(DateUtils.formatDate(bean.getFromDateTime(), "MM月dd日 HH:mm") + " 至 " + DateUtils.formatDate(bean.getToDateTime(), "MM月dd日 HH:mm"));
            }
            if (TextUtils.isEmpty(bean.getFromAirportName())) {
                binding.tvFlightNameTo.setText(bean.getAirlineName() + bean.getFlightNo());
            } else {
                binding.tvFlightNameTo.setText(bean.getAirlineName() + bean.getFlightNo() + " | " + bean.getFromAirportName() + bean.getFromTerminal() + "出发");
            }
            Glide.with(context).load(bean.getLogo()).into(binding.ivLogoTo);

        } else if (entity.getTicketsFlights() != null && entity.getTicketsFlights().size() == 2) {
            binding.clFlightBack.setVisibility(View.VISIBLE);
            binding.tvTo.setVisibility(View.VISIBLE);
            FlightOrdersInfoEntity.TicketsFlightsBean bean = entity.getTicketsFlights().get(0);
            FlightOrdersInfoEntity.TicketsFlightsBean bean1 = entity.getTicketsFlights().get(1);
            if (bean.getTicketsType() == 1) {//去程
                binding.tvCityTo.setText(bean.getFromCity() + " → " + bean.getToCity());

                String startDate = DateUtils.formatDate(bean.getFromDateTime(), "MM月dd日");
                String arriveDate = DateUtils.formatDate(bean.getToDateTime(), "MM月dd日");
                if (startDate.equals(arriveDate)) {
                    binding.tvTimeTo.setText(DateUtils.formatDate(bean.getFromDateTime(), "MM月dd日 HH:mm") + " 至 " + DateUtils.formatDate(bean.getToDateTime(), "HH:mm"));
                } else {
                    binding.tvTimeTo.setText(DateUtils.formatDate(bean.getFromDateTime(), "MM月dd日 HH:mm") + " 至 " + DateUtils.formatDate(bean.getToDateTime(), "MM月dd日 HH:mm"));
                }

                if (TextUtils.isEmpty(bean.getFromAirportName())) {
                    binding.tvFlightNameTo.setText(bean.getAirlineName() + bean.getFlightNo());
                } else {
                    binding.tvFlightNameTo.setText(bean.getAirlineName() + bean.getFlightNo() + " | " + bean.getFromAirportName() + bean.getFromTerminal() + "出发");
                }
                Glide.with(context).load(bean.getLogo()).into(binding.ivLogoTo);

                binding.tvCityBack.setText(bean1.getFromCity() + " → " + bean1.getToCity());

                String startDate1 = DateUtils.formatDate(bean1.getFromDateTime(), "MM月dd日");
                String arriveDate1 = DateUtils.formatDate(bean1.getToDateTime(), "MM月dd日");

                if (startDate1.equals(arriveDate1)) {
                    binding.tvTimeBack.setText(DateUtils.formatDate(bean1.getFromDateTime(), "MM月dd日 HH:mm") + " 至 " + DateUtils.formatDate(bean1.getToDateTime(), "HH:mm"));
                } else {
                    binding.tvTimeBack.setText(DateUtils.formatDate(bean1.getFromDateTime(), "MM月dd日 HH:mm") + " 至 " + DateUtils.formatDate(bean1.getToDateTime(), "MM月dd日 HH:mm"));
                }
                if (TextUtils.isEmpty(bean1.getFromAirportName())) {
                    binding.tvFlightNameBack.setText(bean1.getAirlineName() + bean1.getFlightNo());
                } else {
                    binding.tvFlightNameBack.setText(bean1.getAirlineName() + bean1.getFlightNo() + " | " + bean1.getFromAirportName() + bean1.getFromTerminal() + "出发");
                }
                Glide.with(context).load(bean1.getLogo()).into(binding.ivLogoBack);
            } else {
                binding.tvCityTo.setText(bean1.getFromCity() + " → " + bean1.getToCity());
                String startDate = DateUtils.formatDate(bean1.getFromDateTime(), "MM月dd日");
                String arriveDate = DateUtils.formatDate(bean1.getToDateTime(), "MM月dd日");
                if (startDate.equals(arriveDate)) {
                    binding.tvTimeTo.setText(DateUtils.formatDate(bean1.getFromDateTime(), "MM月dd日 HH:mm") + " 至 " + DateUtils.formatDate(bean1.getToDateTime(), "HH:mm"));
                } else {
                    binding.tvTimeTo.setText(DateUtils.formatDate(bean1.getFromDateTime(), "MM月dd日 HH:mm") + " 至 " + DateUtils.formatDate(bean1.getToDateTime(), "MM月dd日 HH:mm"));
                }
                if (TextUtils.isEmpty(bean1.getFromAirportName())) {
                    binding.tvFlightNameTo.setText(bean1.getAirlineName() + bean1.getFlightNo());
                } else {
                    binding.tvFlightNameTo.setText(bean1.getAirlineName() + bean1.getFlightNo() + " | " + bean1.getFromAirportName() + bean1.getFromTerminal() + "出发");
                }
                Glide.with(context).load(bean1.getLogo()).into(binding.ivLogoTo);

                binding.tvCityBack.setText(bean.getFromCity() + " → " + bean.getToCity());

                String startDate1 = DateUtils.formatDate(bean.getFromDateTime(), "MM月dd日");
                String arriveDate1 = DateUtils.formatDate(bean.getToDateTime(), "MM月dd日");
                if (startDate1.equals(arriveDate1)) {
                    binding.tvTimeBack.setText(DateUtils.formatDate(bean.getFromDateTime(), "MM月dd日 HH:mm") + " 至 " + DateUtils.formatDate(bean.getToDateTime(), "HH:mm"));
                } else {
                    binding.tvTimeBack.setText(DateUtils.formatDate(bean.getFromDateTime(), "MM月dd日 HH:mm") + " 至 " + DateUtils.formatDate(bean.getToDateTime(), "MM月dd日 HH:mm"));
                }
                if (TextUtils.isEmpty(bean.getFromAirportName())) {
                    binding.tvFlightNameBack.setText(bean.getAirlineName() + bean.getFlightNo());
                } else {
                    binding.tvFlightNameBack.setText(bean.getAirlineName() + bean.getFlightNo() + " | " + bean.getFromAirportName() + bean.getFromTerminal() + "出发");
                }
                Glide.with(context).load(bean.getLogo()).into(binding.ivLogoBack);
            }

            binding.tvOrderPrice.setText("¥" + NumberUtil.format((bean.getTicketAmount() + bean1.getTicketAmount()), 2));
        }
    }

    public interface OnClickItem {
        void onApplyPay(FlightOrdersInfoEntity entity);//申请付款
    }

    OnClickItem onClickItem;

    public void setOnClickItem(OnClickItem onClickItem) {
        this.onClickItem = onClickItem;
    }

    public void setCancelTimer() {
        if (allTimer != null && allTimer.size() > 0) {
            for (CountDownTimer countDownTimer : allTimer) {
                countDownTimer.cancel();
            }
            allTimer.clear();
        }

    }
}
